package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repository.StrategyCatalogRepsitory;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DBHelper;
import cn.wolfcode.wolf2w.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogRepsitory strategyCatalogRepsitory;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyService strategyService;

    //容易让mongodb产生误会,后续自动生成的id不会存进去
    @Override
    public void save(StrategyCatalog strategyCatalog) {
        strategyCatalog.setId(null);
        strategyCatalogRepsitory.save(strategyCatalog);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {
        strategyCatalogRepsitory.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {
        strategyCatalogRepsitory.deleteById(id);
    }

    @Override
    public StrategyCatalog get(String id) {
        return strategyCatalogRepsitory.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> list() {
        return strategyCatalogRepsitory.findAll();
    }


    //页面数据
    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize()
                , Sort.Direction.ASC, "_id");
        return DBHelper.query(query, StrategyCatalog.class, template, pageable);
    }


    //添加或修改
    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {
        //把关联目的地设置进去
        String destId = strategyCatalog.getDestId();
        Destination destination = destinationService.get(destId);
        strategyCatalog.setDestName(destination.getName());
        if (!StringUtils.hasLength(strategyCatalog.getId())) {
            this.save(strategyCatalog);
        } else {
            this.update(strategyCatalog);
        }


    }


    //查询分组下拉框数据
    @Override
    public List<CatalogVO> queryGroup() {
        List<CatalogVO> list = new ArrayList<>();
        //聚合查询
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg, Map.class);
        List<Map> datas = result.getMappedResults(); //将结果转化成list<map>   map描述一行数据
        for (Map data : datas) {
            CatalogVO vo = new CatalogVO();

            vo.setDestName(data.get("_id").toString());  //id
            List<Object> names = (List<Object>) data.get("names");  //攻略分类的名称集合
            List<Object> ids = (List<Object>) data.get("ids");  //攻略分类的ids集合

            List<StrategyCatalog> catalogList = new ArrayList<>(); //攻略分类对象

            for (int i = 0; i < names.size(); i++) {
                StrategyCatalog sc = new StrategyCatalog();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                sc.setId(id);
                sc.setName(name);
                catalogList.add(sc);
            }
            vo.setCatalogList(catalogList);
            list.add(vo);
        }
        return list;
    }

    //通过目的地id查询分类数据
    @Override
    public List<StrategyCatalog> queryCatalogByDestId(String destId) {
        //查询所有分类数据
        List<StrategyCatalog> catalogs = strategyCatalogRepsitory.findByDestId(destId);
        //查询出分类数据下每个数据
        for (StrategyCatalog catalog : catalogs) {
            List<Strategy> strategyList = strategyService.QueryByCatalogId(catalog.getId());
            catalog.setStrategies(strategyList);
        }
        return catalogs;
    }
}
